package br.com.android.aluno.overlay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class OverlayCustom extends Overlay {

	public enum TipoOverlay{
		IMAGEM, BOLINHA;
	}
	
	private GeoPoint geoPoint;
	private int id; //cor para desenhar o circulo
	private Paint paint;
	private TipoOverlay tipoOverlay;
	

	/**
	 * [Contrutor padrao] Cria um overlay no formato de circulo no mapa
	 * 
	 * @param geoPoint
	 */
	public OverlayCustom(GeoPoint geoPoint) {
		this.geoPoint = geoPoint;
		this.id = Color.RED;
		this.tipoOverlay = TipoOverlay.BOLINHA;
		this.paint = new Paint();
	}
	
	/**
	 * Cria um overlay no formato de circulo/imagem no mapa
	 * 
	 * @param geoPoint
	 * @param id : cor da bolinha ou res.id da imagem
	 * @param tipoOverlay
	 */
	public OverlayCustom(GeoPoint geoPoint, int id, TipoOverlay tipoOverlay) {
		this.geoPoint = geoPoint;
		this.id = id;
		this.tipoOverlay = tipoOverlay;
		this.paint = new Paint();
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		
		switch (tipoOverlay) {
		case IMAGEM:
			drawImagem(canvas, mapView);
			break;

		default:
			drawBolinha(canvas, mapView);
			break;
		}
	}
	
	private void drawBolinha(Canvas canvas, MapView mapView) {
		if(geoPoint != null){
			paint.setColor(id);
			
			//converte as coordenadas (latitude e longitude) para pixels
			Point ponto = mapView.getProjection().toPixels(geoPoint, null);
			canvas.drawCircle(ponto.x, ponto.y, 5, paint);
		}
	}
	
	private void drawImagem(Canvas canvas, MapView mapView) {
		if(geoPoint != null){			
			//converte as coordenadas (latitude e longitude) para pixels
			Point p = mapView.getProjection().toPixels(geoPoint, null);
			
			Bitmap bitmap = BitmapFactory.decodeResource(mapView.getResources(), this.id);
//			RectF r = new RectF(p.x, p.y, p.x+bitmap.getWidth(), p.y+bitmap.getHeight());	//representa um retangulo com as coordenadas x e y do ponto
			RectF r = new RectF(p.x-bitmap.getWidth(), p.y-bitmap.getHeight(), p.x, p.y);
			canvas.drawBitmap(bitmap, null, r, paint);
		}
	}
	
	public void setGeoPoint(GeoPoint geoPoint){
		this.geoPoint = geoPoint;
	}
}
